package advance.app.service.impl;

import advance.app.dao.SopFileInfoMapper;
import advance.app.pojo.SopFileInfo;
import advance.app.service.SopFileInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class SopFileInfoServiceImpl implements SopFileInfoService {

    @Autowired
    SopFileInfoMapper sopFileInfoMapper;

    public int saveSopFileInfo(SopFileInfo info){
        SopFileInfo sopInfo = new SopFileInfo();
        int rs = 0;
        if(null != info && info.getSopFileType() != null && info.getSopVersion() != null){
            // 删除相同版本的SOP信息;
            int index = sopFileInfoMapper.deleteByTypeAndVer(info.getSopArea(),info.getSopFileType(), info.getSopVersion(),info.getSopFileName());
            sopInfo = sopFileInfoMapper.getByTypeAndVer(info.getSopArea(),info.getSopFileType(), info.getSopVersion(), info.getSopFileName());
            if(null == sopInfo){
                rs = sopFileInfoMapper.saveSopFileInfo(info);
            }else{
                info.setId(sopInfo.getId());
                rs = sopFileInfoMapper.updateSopFileInfo(info);
            }
        }
        return rs;
    }

    public SopFileInfo getLatestInfoByType(String area,String fileType){
        return sopFileInfoMapper.getLatestInfoByType(area,fileType);
    }

    public List<String> getAllVerInfoByType(String area,String fileType){
        return sopFileInfoMapper.getAllVerInfoByType(area,fileType);
    }

    public SopFileInfo getLatestInfoByMid(Integer id){
        return sopFileInfoMapper.getLatestInfoByMid(id);
    }

    @Override
    public int updateAppFileInfo(SopFileInfo info) {
        return sopFileInfoMapper.updateSopFileInfo(info);
    }

    public SopFileInfo getByTypeAndVer(String area,String type, String version,String fileName){
        return sopFileInfoMapper.getByTypeAndVer(area,type,version,fileName);
    }

    public List<SopFileInfo> getByAreaAndTypeAndVer(String area,String type, String version){
        return sopFileInfoMapper.getByAreaAndTypeAndVer(area,type,version);
    }

    @Override
    public int deleteByTypeAndVer(String area,String fileType, String version,String fileName) {
        return 0;
    }

}
